import { $t } from '@vben/locales';

import { updateCorpus } from '#/api/agent/corpus';
import switchStatus from '#/components/FosoSwitch/index.vue';

// 表单配置
export const FormSchemas = [
  {
    fieldName: 'title',
    rules: 'required',
  },
  {
    fieldName: 'content',
    rules: 'required',
  },
  {
    fieldName: 'category',
    rules: 'required',
  },

  {
    component: 'RadioGroup',
    defaultValue: '1',
    componentProps: {
      isButton: true,
      options: [
        {
          label: $t('sys.role.start'),
          value: '1',
        },
        {
          label: $t('sys.role.stop'),
          value: '0',
        },
      ],
    },
    fieldName: 'status',
  },
];
// 表格列配置
export const tableColumns = computed(() => [
  {
    type: 'checkbox',
    width: 60,
  },
  {
    title: $t('customer.corpus.title'),
    field: 'title',
    minWidth: 120,
    treeNode: true,
  },
  {
    title: $t('customer.corpus.content'),
    field: 'content',
    minWidth: 120,
  },
  {
    title: $t('customer.corpus.category'),
    field: 'category',
    minWidth: 120,
  },
  {
    title: $t('common.status'),
    field: 'status',
    minWidth: 120,
    slots: {
      default: ({ row }: { row: any }) => {
        return h(switchStatus, {
          // 1：启动 0：禁用
          status: row.status,
          /**
           * @todo 切换状态报错
           */
          'onUpdate:status': async (value: any) => {
            await updateCorpus({
              id: row.id,
              title: row.title,
              content: row.content,
              category: row.category,
              status: value,
            });
            row.status = value;
            ElMessage.success($t('common.operateSuccess'));
          },
        });
      },
    },
  },
  {
    title: $t('customer.corpus.createTime'),
    field: 'createdAt',
    minWidth: 120,
    formatter: 'formatDateTime',
  },
  {
    title: $t('customer.corpus.updateTime'),
    field: 'updatedAt',
    minWidth: 120,
    formatter: 'formatDateTime',
  },
  // 操作
  {
    title: $t('sys.role.operation'),
    minWidth: 200,
    slots: { default: 'action' },
  },
]);
